package love.yuanmengrobot.qianyugarden.controller;

import love.yuanmengrobot.qianyugarden.entity.Favorites;
import love.yuanmengrobot.qianyugarden.entity.User;
import love.yuanmengrobot.qianyugarden.entity.UserEvents;
import love.yuanmengrobot.qianyugarden.service.*;
import love.yuanmengrobot.qianyugarden.utils.DataGridView;
import love.yuanmengrobot.qianyugarden.utils.RandomUtils;
import love.yuanmengrobot.qianyugarden.vo.MyGarden;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * (UserEvents)表控制层
 *
 * @author COOLIGHT
 * @since 2021-07-13 14:00:42
 */
@Controller
@RequestMapping("userEvents")
public class UserEventsController {
    /**
     * 个人园子动态资源字段定义
     */
    private static final String USER_ID = "UserInfo";
    private static final String USER = "user";
    private static final String PERSONAL_MENU = "personalMenu";
    private static final String BOOK_LIST = "bookList";
    private static final String FANS_NUM = "fansNum";
    private static final String FOllOW_NUM = "followNum";
    private static final String LIKED_NUM = "likedNum";
    private static final String MENUITEM_LIST = "menuItemList";
    private static final String RELEASE_NUM = "releaseNum";
    private static final String UPLOAD_NUM = "uploadNum";
    private static final String YUAN_WEN_LIST = "yuanWenList";

    /**
     * 收藏夹动态资源字段定义
     */
    private static final String COLLECTION_MENU = "collection_menu";
    private static final String SUCCESS = "SUCCESS";
    private static final String ERROR = "ERROR";

    /**
     * 服务对象
     */
    private final UserEventsService userEventsService;
    private final FavoritesService favoritesService;
    private final YuanwenService yuanwenService;

    /*Session中的用户id*/
    private String id = null;

    public UserEventsController(UserEventsService userEventsService, FavoritesService favoritesService, YuanwenService yuanwenService) {
        this.userEventsService = userEventsService;
        this.favoritesService = favoritesService;
        this.yuanwenService = yuanwenService;
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    public UserEvents selectOne(String id) {
        return this.userEventsService.queryById(id);
    }

    /**
     * 加载我的园子页面
     *
     * @param request 内置对象传递数据
     * @return 页面路径
     */
    @RequestMapping("loadMyGarden")
    public String loadMyGarden(HttpServletRequest request, Model model) {
        /*加载数据（下面一小段中带//的内容可以session可用后删除）*/
        HttpSession session = request.getSession(false);
        if (session != null) {//
//            id = session.getAttribute(USER_ID).toString();
            User user = (User) session.getAttribute(USER_ID);
            if (user != null) {
                id = user.getObjectid();
            } else {
                return "redirect:/bus/toLogin";
            }

        } else {
            return "redirect:/bus/toLogin";
        }

//        if ("".equals(id) || id == null) {//
//            id = "2";//
//        }//

        MyGarden myGarden = userEventsService.loadMyGarden(id);

        model.addAttribute(USER, myGarden.getUser());
        model.addAttribute(PERSONAL_MENU, myGarden.getPersonalMenu());
        model.addAttribute(BOOK_LIST, myGarden.getBookList());
        model.addAttribute(FANS_NUM, myGarden.getFansNum());
        model.addAttribute(FOllOW_NUM, myGarden.getFollowNum());
        model.addAttribute(LIKED_NUM, myGarden.getLikedNum());
        model.addAttribute(MENUITEM_LIST, myGarden.getMenuItemList());
        model.addAttribute(RELEASE_NUM, myGarden.getReleaseNum());
        model.addAttribute(UPLOAD_NUM, myGarden.getUploadNum());
        model.addAttribute(YUAN_WEN_LIST, myGarden.getYuanwenList());
        return "myGarden/mygarden";
    }

    @GetMapping("collection")
    public String loadCollection(Model model, HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute(USER_ID);
        if (user != null) {
            id = user.getObjectid();
        }
        model.addAttribute(COLLECTION_MENU, favoritesService.getMenu(id));
        return "myGarden/collection";
    }

    /**
     * 新建收藏夹
     *
     * @param folderName 收藏夹名字
     * @return SUCCESS 成功,ERROR 失败
     */
    @RequestMapping("insertFolder")
    @ResponseBody
    public String insertFolder(String folderName) {
        Favorites favorites = new Favorites();
        favorites.setObjectid(RandomUtils.createIdOX("COOLIGHT"));
        favorites.setUserid(id);
        favorites.setFavoritename(folderName);
        if (favoritesService.insert(favorites) == null) {
            return ERROR;
        }
        return SUCCESS;
    }

    @GetMapping("getFolderContent")
    @ResponseBody
    public DataGridView getFolderContent(String objectId) {
        return new DataGridView(favoritesService.getContent(objectId));
    }
}
